package com.chaodev.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ActivityNoticeDto;
import com.chaodev.base.dto.ActivitySignUpDto;
import com.chaodev.base.model.*;
import com.chaodev.base.service.ActivityServie;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/17 17:15
 **/
@RestController
@RequestMapping("act")
public class ActivityController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ActivityServie activityServie;

    @PostMapping("/getActivityNotList")
    @ApiOperation("获取活动通告")
    public Result getActivityNot(@RequestBody ActivityNoticeDto dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<ActivityNotice> activityNot = activityServie.queryPageList(dto);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取数据成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping("/getActivityNotListOut")
    @ApiOperation("获取活动通告(无token)")
    public Result getActivityNotOut(@RequestBody ActivityNoticeDto dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dto.setActivityTime(dateFormat.format(new Date()));
        dto.setDataType("11");
        PageInfo<ActivityNotice> activityNot = activityServie.queryPageList(dto);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("获取数据成功");
        result.setData(activityNot);
        logService.logAdd(null, "获取活动通告", "SUCCESS", result.toString(), null, request);
        return result;
    }

    @PostMapping("/getMyActivityNotList")
    @ApiOperation("获取我的活动通告")
    public Result getMyActivityNot(@RequestBody ActivityNoticeDto dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            ActivityNotice activityNotice = new ActivityNotice();
            BeanUtil.copyProperties(dto, activityNotice);
            activityNotice.setCUser(userInfo.getUserId());
            PageInfo<ActivityNotice> activityNot = activityServie.queryMyPageList(dto.getPage(), dto.getRows(), activityNotice);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取数据成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取我的活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取我的活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping("/getMyNoSignUpActivity")
    @ApiOperation("获取我未参与过的活动通告")
    public Result getMyNoSignUpActivity(@RequestBody ActivityNoticeDto dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            ActivityNotice activityNotice = new ActivityNotice();
            BeanUtil.copyProperties(dto, activityNotice);
            activityNotice.setCUser(userInfo.getUserId());
            activityNotice.setDataType("10");
            PageInfo<ActivityNotice> activityNot = activityServie.getMyNoSignUpActivity(dto.getPage(), dto.getRows(), activityNotice);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取数据成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取我的活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取我的活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }


    @GetMapping("/delActivityNot")
    @ApiOperation("删除活动通告")
    public Result delActivityNot(String id, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityServie.deleteDataById(id);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("删除成功");
            logService.logAdd(null, "删除活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }


    @PostMapping("/saveData")
    @ApiOperation("新增活动通告")
    public Result addActivityNot(@RequestBody ActivityNotice dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (StrUtil.isEmpty(dto.getCUser())) {
                dto.setCUser(userInfo.getUserId());
            }
            activityServie.saveData(dto, userInfo);
            result.setCode(ResultEnum.SUCCESS.getCode());
            logService.logAdd(null, "新增活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "新增活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @GetMapping("/getById")
    @ApiOperation("通过ID获取活动通告对象")
    public Result findById(String id, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            ActivityNotice actdata = activityServie.findDataById(id);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(actdata);
            logService.logAdd(null, "通过Id获取活动通告表信息", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "通过Id获取活动通告表信息", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @GetMapping("/getByIdOut")
    @ApiOperation("通过ID获取活动通告对象(无token)")
    public Result findByIdOut(String id, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        ActivityNotice actdata = activityServie.findDataById(id);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setData(actdata);
        logService.logAdd(null, "通过Id获取活动通告表信息", "SUCCESS", result.toString(), null, request);
        return result;
    }

    @PostMapping("/getByKeyWord")
    @ApiOperation("通过关键字获取活动通告对象集合")
    public Result findByKey(@RequestBody ActivityNotice dto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            List<ActivityNotice> actdata = activityServie.queryDataList(dto);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(actdata);
            logService.logAdd(null, "通过关键字获取活动通告对象集合", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "通过关键字获取活动通告对象集合", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取活动报名表列表分页信息
     *
     * @author nw
     */
    @ApiOperation(value = "获取活动报名表列表分页", notes = "获取活动报名表列表分页信息")
    @PostMapping(value = {"/getSignUplist"})
    public Result list(@RequestBody ActivitySignUpDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<ActivitySignUp> activityNot = activityServie.querySignUpPageList(dto);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "获取活动报名表列表分页(去token)", notes = "获取活动报名表列表分页信息")
    @PostMapping(value = {"/getSignUplistOut"})
    public Result listOut(@RequestBody ActivitySignUpDto dto, HttpServletRequest request) {
        Result result = new Result();
        PageInfo<ActivitySignUp> activityNot = activityServie.querySignUpPageList(dto);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("获取成功");
        result.setData(activityNot);
        logService.logAdd(null, "获取活动通告", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 获取活动报名表信息
     *
     * @author nw
     */
    @ApiOperation(value = "获取活动报名表对象", notes = "获取活动报名表信息")
    @GetMapping(value = {"/getSignUpById"})
    public Result findById(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            ActivitySignUp tbActivitySignUp = activityServie.findSignUpDataById(id);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(tbActivitySignUp);
            logService.logAdd(null, "获取活动报名表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动报名表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存活动报名表信息
     *
     * @author nw
     */
    @ApiOperation(value = "保存活动报名表对象", notes = "保存活动报名表信息")
    @PostMapping(value = {"/saveSignUp"})
    public Result save(@RequestBody ActivitySignUp activitySignUp, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (StrUtil.isEmpty(activitySignUp.getSignUpUserCode())) {
                activitySignUp.setSignUpUserCode(userInfo.getUserId());
            }
            if (StrUtil.isEmpty(activitySignUp.getSignUpUserName())) {
                activitySignUp.setSignUpUserName(userInfo.getUserName());
            }
            activityServie.saveSignUpData(activitySignUp);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("操作成功");
            result.setData(activitySignUp);
            logService.logAdd(null, "保存活动报名表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存活动报名表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "保存活动报名表对象(无token)", notes = "保存活动报名表信息(无token)")
    @PostMapping(value = {"/saveSignUpOut"})
    public Result saveOut(@RequestBody ActivitySignUp activitySignUp, HttpServletRequest request) {
        Result result = new Result();
        if (StrUtil.isEmpty(activitySignUp.getSignUpUserCode())) {
            activitySignUp.setSignUpUserCode(activitySignUp.getPhone());
        }
        activityServie.saveSignUpData(activitySignUp);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("操作成功");
        result.setData(activitySignUp);
        logService.logAdd(null, "保存活动报名表对象", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 删除活动报名表信息
     *
     * @author nw
     */
    @ApiOperation(value = "删除活动报名表对象", notes = "删除活动报名表信息")
    @GetMapping(value = {"/delSignUp"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityServie.deleteSignUpDataById(id);
            result.setCode(ResultEnum.SUCCESS.getCode());
            logService.logAdd(null, "删除活动报名表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除活动报名表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping("getMySignUpAct")
    @ApiOperation("获取我报名的活动")
    public Result getMySignUpAct(@RequestBody ActivityNoticeDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<ActivityNotice> activityNot = activityServie.getMySignUpAct(dto, userInfo);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取数据成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取活动通告", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动通告", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
